package com.potevio.simulator.task;


import com.potevio.simulator.common.CommonFlag;
import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.heart.HeartbeatClient;
import io.netty.channel.Channel;
import org.springframework.util.CollectionUtils;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName CommonTask
 * @Description: 定时任务公共类
 * @Date 2021/6/24  13:17
 * @Author zyd
 */
public class CommonTask {

    public void startChargeMsgTask(Channel channel){
        try {
            // 开启远端充电过程报文定时任务
            ChargeMsgTask chargeMsgTask = new ChargeMsgTask();
            // scheduleAtFixedRate(执行线程，初始化延时，两次开始执行最小间隔时间，计时单位)
            HeartbeatClient.chargeMsgTaskFuture = channel.eventLoop().scheduleAtFixedRate(chargeMsgTask, 0, HeartbeatClient.chargeMsgInterval, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改充电桩状态为启动在线
     */
    public void updateChargeState(){
        // 设置充电桩状态为启动
        if(CollectionUtils.isEmpty(MsgTypeConst.CHARGE_POSTS)){
            HeartbeatClient.chargingPileState = 0;
        }
        ChargeMsgTask.stopFlag = true;
    }

    /**
     * 停止充电报文任务
     */
    public void stopChargeMsgTask(){
        HeartbeatClient.chargeMsgTaskFuture.cancel(false);
    }
}
